import { Label } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { App } from '../../App';
import UtilTool from '../../tools/UtilTool';
import CommConst from '../../const/CommConst';
import wsTool from '../../net/ws';
import { Button } from 'cc';
import { Sprite } from 'cc';
import { MultiMsgMgr } from './MultiMsgMgr';
import { showErrorTip } from '../../tools/PopuUp';
const { ccclass, property } = _decorator;

@ccclass('JoinItem')
export class JoinItem extends Component {

    @property(Label)
    label_user : Label = null; // 用户名

    @property(Node)
    masterNode : Node = null; // 房主标签

    @property(Label)
    label_mine : Label = null; // 我自己标签

    @property(Button) 
    btn_remove : Button = null // 移除按钮

    @property(Node) 
    hasReadyNode : Node = null; // 已参数

    member = null; // 成员地址

    isMaster : boolean = false; // 是否房主
    isMine : boolean = false; // 是否自己
    
    start() {

    }

    initData(member, masterMember, isMaster, isMine, isHasReady, nickname) {
        this.member = member;
        this.isMaster = isMaster
        this.isMine = isMine

        // 用户：2a5s2...3a6s
        this.label_user.string = App.language.getLanguageLab(130) + UtilTool.formatNickOrAddress({"nickname":nickname, "address":member, "addressLen":6})

        this.label_mine.node.active = false;
        this.masterNode.active = false;
        this.btn_remove.node.active = false;
        this.hasReadyNode.active = isHasReady;

        let isMasterMemeber = masterMember == member

        // 房主标签
        if(isMasterMemeber) {
            this.masterNode.active = true;
            return
        }

        // 移除按钮 （房主权限）
        if(this.isMaster) {

            // 如果有人准备了，就不显示移除按钮
            if(!isHasReady) {
                this.btn_remove.node.active = true;
            }
            return
        }

        // 我自己
        if(this.isMine) {
            this.label_mine.node.active = true;
            return
        }

    }

    // 房主移除按钮
    onBtnRemove() {

        MultiMsgMgr.getInstance().sendKickOut(this.member)
        .then(data => { 
            UtilTool.printLog("test sendKickOut", data);

            if(data.result != 0) {
                showErrorTip(data.result)
            }

        })

    }

}

